
package com.b1.taskmanager.parser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.SAXException;

/**
 * Класс для чтения и записи в contact.xml
 */
public class TaskXml 
{
    private String[][] taskArray;
    private File file = new File("task.xml");
    
    public String[][] getTaskArray() 
    {
        return taskArray;
    }

    /*
     * Конструктор
     * инициализирует массив размерности = количеству записей и заполняет его
     */
    public TaskXml() throws IOException, ParserConfigurationException, SAXException
    {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        document.getDocumentElement().normalize();
        NodeList nodeList = document.getElementsByTagName(document.getDocumentElement().getChildNodes().item(1).getNodeName());
        taskArray = new String [nodeList.getLength()][8];
        readXml(nodeList);
    }
    
    private void readXml(NodeList nodeList) 
    {        
        for(int i = 0; i < nodeList.getLength(); i++)
            {                
                Node node = nodeList.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element element = (Element)node;
                    taskArray[i][0] = node.getAttributes().item(0).getTextContent();
                    taskArray[i][1] = element.getElementsByTagName("begin").item(0).getChildNodes().item(0).getNodeValue();
                    taskArray[i][2] = element.getElementsByTagName("end").item(0).getChildNodes().item(0).getNodeValue();
                    taskArray[i][3] = element.getElementsByTagName("title").item(0).getChildNodes().item(0).getNodeValue();
                    taskArray[i][4] = element.getElementsByTagName("type").item(0).getChildNodes().item(0).getNodeValue();
                    if (taskArray[i][4].equals("meeting"))taskArray[i][5] = element.getElementsByTagName("place").item(0).getChildNodes().item(0).getNodeValue();
                    else taskArray[i][5] = "-";
                    taskArray[i][6] = element.getElementsByTagName("contacts").item(0).getAttributes().item(0).getTextContent();
                    taskArray[i][7] = element.getElementsByTagName("description").item(0).getChildNodes().item(0).getNodeValue();   
                }
            }
    }
    
    /**
     * Процедура записи task's в xml
     */
    public void writeXml(String taskArray[][]) throws IOException
    {
        org.jdom2.Element rootElement = new org.jdom2.Element("tasklist");
        org.jdom2.Element contactElement[] = new org.jdom2.Element[taskArray.length];
        org.jdom2.Element childElement;
        for (int i = 0; i < taskArray.length; i++) 
        {
            contactElement[i] = new org.jdom2.Element("task");
            contactElement[i].setAttribute("id", taskArray[i][0]);
            childElement = new org.jdom2.Element("begin");
            if ((taskArray[i][1]==null)||(taskArray[i][1].equals(""))) childElement.addContent("null");
            else childElement.addContent(taskArray[i][1]);
            contactElement[i].addContent(childElement);
            
            childElement = new org.jdom2.Element("end");
            if ((taskArray[i][2]==null)||(taskArray[i][2].equals(""))) childElement.addContent("null");
            else childElement.addContent(taskArray[i][2]);
            contactElement[i].addContent(childElement);
            
            childElement = new org.jdom2.Element("title");
            if ((taskArray[i][3]==null)||(taskArray[i][3].equals(""))) childElement.addContent("null");
            else childElement.addContent(taskArray[i][3]);
            contactElement[i].addContent(childElement);
            
            childElement = new org.jdom2.Element("type");
            if ((taskArray[i][4]==null)||(taskArray[i][4].equals(""))) childElement.addContent("null");
            else childElement.addContent(taskArray[i][4]);
            contactElement[i].addContent(childElement);
            
            childElement = new org.jdom2.Element("place");
            if ((taskArray[i][5]==null)||(taskArray[i][5].equals(""))) childElement.addContent("null");
            else childElement.addContent(taskArray[i][5]);
            contactElement[i].addContent(childElement);
            
            childElement = new org.jdom2.Element("contacts");
            if ((taskArray[i][6]==null)||(taskArray[i][6].equals(""))) childElement.setAttribute("id","null");
            else childElement.setAttribute("id",taskArray[i][6]);
            contactElement[i].addContent(childElement);
            
            childElement = new org.jdom2.Element("description");
            if ((taskArray[i][7]==null)||(taskArray[i][7].equals(""))) childElement.addContent("null");
            else childElement.addContent(taskArray[i][7]);
            contactElement[i].addContent(childElement);
        }
        for (int i = 0; i < taskArray.length; i++) 
        {
            rootElement.addContent(contactElement[i]);
        }
        Format fmt = Format.getPrettyFormat();
        XMLOutputter outputter = new XMLOutputter(fmt);
        outputter.output(new org.jdom2.Document(rootElement), new FileOutputStream (file)); 
    }
    
}
